Categories
Modern JavaScript

Best of Modern JavaScript — Numbers

Spread the love

Since 2015, JavaScript has improved immensely.

It’s much more pleasant to use it now than ever.

In this article, we’ll look at the core features of JavaScript.

New Number Properties

ES6 introduced some new number properties.

New static properties of Number includes Number.EPISLON for comparing floating-point number rounding tolerance.

Number.isInteger checks whether num is an integer.

If we write:

Number.isInteger(2.05)

then it returns false .

If we write:

Number.isInteger(2)

then it returns true .

This also works for negative numbers.

There’s also the isSafeInteger method that determines whether a JavaScript integer is safe.

A safe integer is one that can be represented within the signed 53-bit range without losing precision.

For instance, if we write:

Number.isSafeInteger(2)

then it returns true .

There’s also the Number.MIN_SAFE_INTEGER that has the min value for the safe integer.

And there’s the Number.MAX_SAFE_INTEGER value to get the max value of a safe integer.

ES6 also comes with the Number.isNaN method to check whether num is the value NaN .

Unlike isNaN , it doesn’t coerce its argument into a number before doing the check.

So if we write:

isNaN('foo')

It returns true .

On the other hand, if we write:

Number.isNaN('foo')

Then it returns false .

The Number object also has the Number.isFinite , Number.parseFloat , and Number.parseInt methods.

Number.isFinite checks whether a number is finite.

Number.parseFloat converts a non-number to a floating-point number.

And Number.parseInt converts a non-number to an integer.

These are mostly the same as their global equivalents.

Math Methods

ES6 also added new methods to the Math object.

The Math.sign method returns the sign of a number.

It returns -1 if the number is negative.

It returns 0 if the number is 0.

And it returns 1 if the number is positive.

For instance, we can write:

Math.sign(-10)

and get -1.

If we write:

Math.sign(0)

we get 0.

And if we have:

Math.sign(3)

We get 1.

The Math.trunc method removes the decimal portion of a number.

So if we have:

Math.trunc(2.1)

or

Math.trunc(2.9)

We get 2.

And if we have:

Math.trunc(-2.1)

or:

Math.trunc(-2.9)

We get -2.

The Math.log10 method computes the log with base 10 of a number.

For example, we can write:

Math.log10(1000)

and get 3.

Math.hypot calculates the square root of the number of squares of its arguments.

For example, if we have:

Math.hypot(1, 1)

We get 1.4142135623730951 .

Integer Literals

ES5 introduced hex integers.

So we can write:

0x1F

and get 31 in decimal.

ES6 introduced more kinds of numbers.

Binary literals are one of them.

For example, if we write:

0b111

then we get 7 in decimal.

It also introduced octal literals.

For instance, we can write:

0o101

and get 65 in decimal.

We can use the toString method with numbers with bases that aren’t 10.

For example, we can write:

8..toString(8)

And we get 10 in decimal.

The argument is the radix, or the base to treat the number as.

To call a method with a number literal, we’ve to include an extra dot.

Conclusion

Numbers have can be represented in variables and converted with ES6 or later.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *